﻿using UnityEngine;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using Assets.Game.scripts;
using Engine.Entities.Character;

namespace Game.Scripts.Managers {

    [Serializable]
    [RequireComponent(typeof(BuildingManager))]
    public class ResourceManager : MonoBehaviour {

        public static ResourceManager singleton;

        public static ResourceManager instance {
            get {
                return ResourceManager.singleton;
            }
        }

        public Dictionary<ResourceEnum, string> mappingIconResource = new Dictionary<ResourceEnum, string>();
        public Dictionary<SkillsEnum, string> mappingIconSkill = new Dictionary<SkillsEnum, string>();

        //magazzino totale del mondo di gioco
        public List<Resource> resources = new List<Resource>();

        public void Awake(){

            if (ResourceManager.singleton != null) {
                DestroyImmediate(this);
            } else {
                ResourceManager.singleton = this;
            }

            //per test, riempio automaticamente il mapping delle risorse e delle skill
            mappingIconSkill.Add(SkillsEnum.Strenght, "Letter-S-icon");
            mappingIconSkill.Add(SkillsEnum.Agility, "Letter-A-icon");
            mappingIconSkill.Add(SkillsEnum.Charm, "Letter-C-icon");
            mappingIconSkill.Add(SkillsEnum.Resistance, "Letter-R-icon");
            mappingIconSkill.Add(SkillsEnum.Intelligence, "Letter-I-icon");
            mappingIconResource.Add(ResourceEnum.Wood, "wood");
            mappingIconResource.Add(ResourceEnum.Food, "food");
            mappingIconResource.Add(ResourceEnum.Stone, "stone");
            mappingIconResource.Add(ResourceEnum.Medikit, "medikit");

        }

        public int resourcesCount {
            get {
                return this.resources.Count;
            }
        }

        public Resource getResourceByIndex(int index) {
            return this.resources[index];
        }

        public Resource getResourceByType(ResourceEnum type) {

            Resource prototype = null;

            foreach (Resource p in this.resources) {
                if (p.type == type) {
                    prototype = p;
                    break;
                }
            }

            return prototype;
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool hasAmounts(Resource[] amounts)
        {
            if (amounts == null) return false;
            foreach (Resource item in amounts)
            {
                Resource globalRes = getResourceByType(item.type);
                if (!globalRes.hasAmount(item.amount)) return false;
            }
            return true;
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public void withdraw(Resource[] amounts)
        {
            //ciclo sulle risorse che voglio pagare
            foreach (Resource item in amounts)
            {
                //chiedo la risorsa globale e detraggo
                Resource globalRes = getResourceByType(item.type);
                globalRes.withdraw(item.amount);
            }
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public void deposit(Resource[] amounts)
        {
            //ciclo sulle risorse che voglio pagare
            foreach (Resource item in amounts)
            {
                //chiedo la risorsa globale e detraggo
                Resource globalRes = getResourceByType(item.type);
                globalRes.deposit(item.amount);
            }
        }
    }

}